package com.example.springboot.controller;


import com.example.springboot.common.AuthAccess;
import com.example.springboot.common.Result;
import com.example.springboot.entity.User;
import com.example.springboot.service.UserService;
import com.example.springboot.utils.validationgroup.AddGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin
@RestController
@Api(tags = "登录注册")
public class WebController {

    @Resource
    UserService userService;

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result login(@Validated({AddGroup.class}) @RequestBody User user) {
        user = userService.login(user);
        return Result.success(user);
    }

    @AuthAccess
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public Result register(@Validated({AddGroup.class}) @RequestBody User user) {
        user = userService.register(user);
        return Result.success(user);
    }


    /**
     * Controller重置密码
     */
    @AuthAccess
    @PutMapping("/password")
    @ApiOperation("重置密码")
    public Result password(@RequestBody User user) {
        userService.resetPassword(user);
        return Result.success();
    }

}
